﻿//-----------------------------------------------------------------------
// <copyright file="InternalUtilities.cs" company="COMPANY">
//     Copyright (c) COPYRIGHT. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace CodeQualityExtension.UnitTestRules
{
    using System;
    using CodeQualityExtension.Framework;
    using Microsoft.FxCop.Sdk;
    
    /// <summary>
    /// Utility methods
    /// </summary>
    internal static class InternalUtilities
    {
        /// <summary>
        /// Full name of TestClassAttribute
        /// </summary>
        internal const string TestClassAttributeFullName =
            "Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute";

        /// <summary>
        /// Full name of TestMethodAttribute
        /// </summary>
        internal const string TestMethodAttributeFullName =
            "Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute";
        
        /// <summary>
        /// Determines whether method is marked by TestMethodAttribute. Excludes methods 
        /// from classes, which are not marked by TestClassAttribute and are therefore 
        /// excluded from being executed.
        /// </summary>
        /// <param name="method">FxCop method instance</param>
        /// <returns>True if method is test method</returns>
        internal static bool IsTestMethod(Method method)
        { 
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            bool result = false;
            
            TypeNode typeNode = method.DeclaringType;
            if (typeNode.HasAttribute(TestClassAttributeFullName))
            {
                if (method.HasAttribute(TestMethodAttributeFullName))
                {
                    result = true;
                }
            }

            return result;
        }
    }
}
